package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.rank.UnionActivityRank;
import com.yanqu.road.logic.cross.CrossActivityUnionRankListModel;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.rank.UnionActivityRankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C2_CROSS_GET_TRADE_WAR_ACTIVITY_UNION_RANK, desc = "查看跨服商战活动商会榜单")
public class GetCrossTradeWarActivityUnionRankCmd extends CrossCommand {
    private int activityId;
    private long userId;
    private String unionId;
    private String localValue = "0";

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        userId = packet.getPlayerId();
        CrossActivityProto.CrossUnionActivtyRankReqMsg req = CrossActivityProto.CrossUnionActivtyRankReqMsg.parseFrom(packet.getBytes());
        activityId = req.getActivtyId();
        localValue = req.getLocalValue();
        unionId = req.getUnionId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossActivityUnionRankListModel rankListModel = CrossTradeWarRankMgr.getCrossActivityUnionRankListModel(activityId);
        String unionId = "";
        String localValue = "0";
        UnionActivityConditionData conditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, 0, userId);
        if (conditionData != null) {
            unionId = conditionData.getUnionUid();
            localValue = conditionData.getValue().toString();
        }
        CommonActivityProto.CommonActivityUnionRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityUnionRankListRespMsg.newBuilder();
        int myRank = -1;
        if (rankListModel != null) {
            myRank = rankListModel.getMyRank(serverId, unionId);
            for (UnionActivityRank rank : rankListModel.getRankList()) {
                if (rank.getUnionUid().equals(unionId)) {
                    localValue = rank.getValue().toString();
                }
            }
            respMsg.setRankList(rankListModel.getRankListMsg());
        }else {
            respMsg.setRankList(UnionActivityRankProto.UnionActivityRankListMsg.newBuilder());
        }
        respMsg.setMyRank(myRank);
        respMsg.setValue(localValue);
        YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_TRADE_WAR_ACTIVITY_UNION_RANK, respMsg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
